package com.powernode.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.SysLog;
import com.powernode.model.Result;
import com.powernode.service.SysLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Author: Tong Ziyu
 * @Date: 2025/2/5 11:12
 */
@RestController
@Api(tags = "系统日志接口管理")
@RequestMapping("sys/log")
@Slf4j
public class SysLogController {

    @Autowired
    private SysLogService sysLogService;

    /**
     * 分页查询日志
     * @return
     */
    @ApiOperation("分页查询系统日志")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('sys:log:page')")
    public Result<Page> loadPage(@RequestParam Long current,
                                 @RequestParam Long size,
                                 @RequestParam(required = false) String userId,
                                 @RequestParam(required = false) String operation){
        Page<SysLog> page = new Page<>(current, size);

        page = sysLogService.page(page, new LambdaQueryWrapper<SysLog>()
                        .eq(ObjectUtil.isNotNull(userId),SysLog::getUserId, userId)
                        .like(StringUtils.hasText(userId),SysLog::getOperation, operation)
                        .orderByDesc(SysLog::getCreateDate)
        );

        return Result.success(page);
    }

}
